﻿Imports System.Data.SqlClient
Imports System.Security.Cryptography
Imports System.Text

Public Class NouveauUtil

    Private user As String
    Private userSite As String
    Private cn As CnData
    Private da As SqlDataAdapter
    Private ds As DataSet
    Private cmd As SqlCommand
    Private dtr As SqlDataReader
    Private util As String
    Private mdp As String

    Public Sub New(ByVal user As String, ByRef cn As CnData, ByVal userSite As String, ByVal nom As String, ByVal util As String)
        ' Cet appel est requis par le concepteur.
        InitializeComponent()

        ' Ajoutez une initialisation quelconque après l'appel InitializeComponent().
        Me.user = user
        Me.cn = cn
        Me.userSite = userSite
        Me.Text = nom
        Me.util = util
        mdp = ""
    End Sub


    Private Sub NouveauUtil_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        cn.Open()
        da = New SqlDataAdapter("SELECT Distinct Profil FROM Profils WHERE Profil <> 'Admin'", cn.getCn)
        ds = New DataSet
        da.Fill(ds, "profil")
        With profilv
            .DataSource = ds.Tables("profil")
            .DisplayMember = "Profil"
            .ValueMember = "Profil"
            .SelectedValue = userSite
        End With

        If Me.Text = "Modification" Then
            cmd = New SqlCommand("SELECT * " _
                                            & " FROM Utilisateurs " _
                                            & " WHERE     (Util = '" & util & "') AND DateS IS NULL", cn.getCn)
            dtr = cmd.ExecuteReader
            While dtr.Read()
                nomv.Text = dtr.Item("Nom").ToString
                prenomv.Text = dtr.Item("Prenom").ToString
                utilv.Text = dtr.Item("Util").ToString
                mdpv.Text = dtr.Item("Mdp").ToString
                mdp = dtr.Item("Mdp").ToString
                profilv.SelectedValue = dtr.Item("Profil").ToString
            End While
            dtr.Close()
        End If
        cn.Close()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If String.IsNullOrEmpty(nomv.Text) Or String.IsNullOrEmpty(prenomv.Text) Or String.IsNullOrEmpty(utilv.Text) Or String.IsNullOrEmpty(mdpv.Text) Or String.IsNullOrEmpty(profilv.SelectedValue) Then
            MsgBox("Veuiller remplir tous les champs")
            Exit Sub
        End If
        cn.Open()


        If util <> utilv.Text.ToUpper Then
            cmd = New SqlCommand("SELECT * FROM Utilisateurs WHERE Util='" & utilv.Text.ToUpper & "' AND DateS IS NULL", cn.getCn())

            dtr = cmd.ExecuteReader
            If dtr.Read Then
                MsgBox("Ce nom d'utilisateur existe déjà")
                dtr.Close()
                cn.Close()
                Exit Sub
            End If
            dtr.Close()
        End If
        If Me.Text = "Modification" Then
            If mdp <> mdpv.Text Then
                Dim MD5Pass As String

                Using md5Hash As MD5 = MD5.Create()
                    MD5Pass = GetMd5Hash(md5Hash, mdpv.Text.ToUpper)
                End Using

                cmd = New SqlCommand("UPDATE Utilisateurs SET Nom = '" & nomv.Text & "',Prenom = '" & prenomv.Text & "',Util= '" & utilv.Text.ToUpper & "', DateM='" & Date.Today & "', UtilM='" & user & "',Mdp= '" & MD5Pass & "',Profil='" & profilv.SelectedValue & "' WHERE Util='" & util & "' AND DateS IS NULL", cn.getCn())
                cmd.ExecuteNonQuery()
            Else
                cmd = New SqlCommand("UPDATE Utilisateurs SET Nom = '" & nomv.Text & "',Prenom = '" & prenomv.Text & "',Util= '" & utilv.Text.ToUpper & "', DateM='" & Date.Today & "', UtilM='" & user & "',Profil='" & profilv.SelectedValue & "' WHERE Util='" & util & "' AND DateS IS NULL", cn.getCn())
                cmd.ExecuteNonQuery()
            End If
        Else
            Dim MD5Pass As String

            Using md5Hash As MD5 = MD5.Create()
                MD5Pass = GetMd5Hash(md5Hash, mdpv.Text.ToUpper)
            End Using

            cmd = New SqlCommand("INSERT INTO  Utilisateurs (Nom,Prenom,Site,Util,Mdp,Profil,DateA,Responsable) VALUES ('" & nomv.Text & "','" & prenomv.Text & "','" & userSite & "','" & utilv.Text.ToUpper & "','" & MD5Pass & "','" & profilv.SelectedValue & "','" & Date.Today & "','" & user & "')", cn.getCn())
            cmd.ExecuteNonQuery()
        End If

        cn.Close()
        MsgBox("Compte enregistré")
        Me.DialogResult = Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub


    Shared Function GetMd5Hash(ByVal md5Hash As MD5, ByVal input As String) As String

        ' Convert the input string to a byte array and compute the hash. 
        Dim data As Byte() = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input))

        ' Create a new Stringbuilder to collect the bytes 
        ' and create a string. 
        Dim sBuilder As New StringBuilder()

        ' Loop through each byte of the hashed data  
        ' and format each one as a hexadecimal string. 
        Dim i As Integer
        For i = 0 To data.Length - 1
            sBuilder.Append(data(i).ToString("x2"))
        Next i

        ' Return the hexadecimal string. 
        Return sBuilder.ToString()

    End Function

    Private Sub Fermer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Fermer.Click
        Me.Close()
    End Sub
End Class